// pages/poetries/poetries.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    poetries: [],
    q: "",
    scrollTop: 0,
    page: 0,
    scrollHeight: 0,
    hasMore: true,
    loadding: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    }); 
    this.loadPoetries()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  listenerSearchInput: function (e) {
    this.setData({
      q: e.detail.value
    })
  },
  toSearch:function(){
    this.loadPoetries()
  },
  bindDownLoad: function (e) {
    console.log(e)
    var that = this;
    that.loadPoetries();
  },
  toSearch: function () {
    const that = this;
    that.setData({
      loadding: true,
      poetries: [],
      page: 0,
    })
    wx.request({
      url: config.service.apiUrl + '/poetries',
      data: {
        q: that.data.q,
        page: that.data.page + 1
      },
      success: function (res) {
        that.setData({
          poetries: that.data.poetries.concat(res.data),
          page: that.data.page + 1,
          hasMore: true,
          loadding: false,
        });
      }
    })
  },
  loadPoetries: function () {
    const that = this;
    that.setData({
      loadding: true,
    })
    wx.request({
      url: config.service.apiUrl + '/poetries',
      data:{
        q:that.data.q,
        page: that.data.page + 1,
      },
      success: function (res) {
        that.setData({
          poetries: that.data.poetries.concat(res.data),
          page: that.data.page + 1,
          hasMore: true,
          loadding: false,
        });
      }
    })
  },
  toPoetry: function (e) {
    wx.navigateTo({
      url: "/pages/poetry/poetry?id=" + e.currentTarget.dataset.id
    })
  }
})